﻿using AntdUI;
using ImportPlcPointPosition.Utils;
using Mapster;
using SqlSugar;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZR.Model.Models.ProductionControl;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace ImportPlcPointPosition.Controls
{
    public partial class PlcManagePage : BaseUserControl
    {

        public PlcManagePage()
        {

            InitializeComponent();

            InitTable();
            this.Load += PlcManagePage_Load;
        }
        SqlSugarClient db = SqlSugarHelper.MesDB;


        private void InitTable()
        {
            table1.EditMode = TEditMode.Click;
            table1.Columns = new ColumnCollection();
            //  table1.Columns.Add(new ColumnCheck("IsCheck") { });  
            table1.Columns.Add(new Column("Id", "ID"));
            table1.Columns.Add(new Column("WorkstationCode", "工站编号"));
            table1.Columns.Add(new Column("Remark", "备注"));
            table1.Columns.Add(new Column("PlcAddress", "IP地址"));
            table1.Columns.Add(new Column("CpuType", "CPU类型"));
            table1.Columns.Add(new Column("RackNumber", "机架号"));
            table1.Columns.Add(new Column("SlotNumber", "插槽号"));
            
            table1.Columns.Add(new ColumnCheck("IsEnabled", "启用状态"));
            table1.Columns.Add(new ColumnCheck("AgvEnable", "AGV功能"));
            table1.Columns.Add(new Column("Operate", "操作") { Fixed = true });
            table1.Bordered = true;
            table1.EnableHeaderResizing = true;
            table1.EmptyHeader = true;
            table1.Gap = 6;

            table1.CellBeginEditInputStyle += Table1_CellBeginEditInputStyle;
            table1.CellEndEdit += Table1_CellEndEdit;
            table1.CellButtonClick += Table1_CellButtonClick;
        }



        private bool Table1_CellEndEdit(object sender, TableEndEditEventArgs e)
        {
            var col = table1.Columns[e.ColumnIndex];
            if (col.Key == "Id")
            {
                var old = e.Record as PlcConfig;
                var newid = e.Value.ObjToInt();
                var one = db.Queryable<PlcConfig>().Where(a => a.Id == e.Value.ObjToInt()).First();
                if (one != null)
                {

                    AntdUI.Message.error(App.MainLayout, "保存失败：Id已经存在", autoClose: 2);
                    return false;
                }
                db.Updateable<PlcConfig>().SetColumns(a => a.Id, newid).Where(a => a.Id == old.Id).ExecuteCommand();


            }

            return true;
        }

        private void Table1_CellBeginEditInputStyle(object sender, TableBeginEditInputStyleEventArgs e)
        {
            e.Input.SelectAll();
        }

        private void PlcManagePage_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        AntList<PlcConfig> Source = new AntList<PlcConfig>();
        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            Source.Clear();
            var db = SqlSugarHelper.MesDB;
            var lst = db.Queryable<PlcConfig>().ToList();
            foreach (var item in lst)
            {
                Source.Add(item);
            }
            this.table1.Binding(Source);
        }

        private void btnNewline_Click(object sender, EventArgs e)
        {
            Source.Add(new PlcConfig());
        }

        private void Table1_CellButtonClick(object sender, TableButtonEventArgs e)
        {

            var item = e.Record as PlcConfig;

            switch (e.Btn.Id)
            {
                case "save":
                    if (item.Id < 1)
                    {
                        AntdUI.Message.error(App.MainLayout, "Id不能小于1", autoClose: 2);
                        return;
                    }
                    var Exists = db.Queryable<PlcConfig>().Where(a => a.Id == item.Id).Any();
                    if (Exists)
                    {
                        db.Updateable<PlcConfig>(item).Where(a => a.Id == item.Id).ExecuteCommand();
                    }
                    else
                    {
                        db.Insertable(item).ExecuteCommand();
                    }
                    AntdUI.Message.info(App.MainLayout, "保存成功", autoClose: 1);
                    break;
                case "del":
                    var result = Modal.open(this.FindForm(), "删除警告！", "确认要删除选择的数据吗？", TType.Warn);
                    if (result == DialogResult.OK)
                    {
                        db.Deleteable<PlcConfig>().WhereColumns(item, a => new { a.Id }).ExecuteCommand();
                        Source.Remove(item);
                    }
                    break;
                default:
                    break;
            }
        }

        private void btnReflush_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
